package com.contractpage.ec.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.dao.AppointDao;
import com.contractpage.ec.dao.ContractDetailDao;
import com.contractpage.ec.dao.ContractInboundDao;
import com.contractpage.ec.dao.ContractStatusHistroyDao;
import com.contractpage.ec.dao.ExpressInboundDao;
import com.contractpage.ec.dao.ExpressInformationDao;
import com.contractpage.ec.entity.Appoint;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.entity.ContractInbound;
import com.contractpage.ec.entity.ContractStatusHistroy;
import com.contractpage.ec.entity.ExpressInbound;
import com.contractpage.ec.entity.ExpressInformation;
import com.contractpage.ec.service.ExpressInformationService;
import com.user.entity.emp.EmpVO;

/**
 * 
 * ExpressInformationServiceImpl数据库操作接口类
 * 
 **/
@Transactional
@Service
public class ExpressInformationServiceImpl implements ExpressInformationService {
	@Autowired
	private ExpressInformationDao expressInformationDao;
	@Autowired
	private ContractInboundDao contractInboundDao;
	@Autowired
	private ContractDetailDao contractDetailDao;
	@Autowired
	private ExpressInboundDao expressInboundDao;
	@Autowired
	private ContractStatusHistroyDao contractStatusHistroyDao;
	@Autowired
	private AppointDao appointDao;

	@Override
	public Page<ExpressInformation> findList(Page<ExpressInformation> page, ExpressInformation entity) {
		entity.setPage(page);
		page.setList(expressInformationDao.findList(entity));
		return page;
	}

	/**
	 * 新增快递
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int insert(ExpressInformation entity) {
		entity.preInsert("");
		Integer sends = 0;
		for (ContractInbound inbound : entity.getInbounds()) {
			ExpressInbound expressInbound = new ExpressInbound();
			expressInbound.preInsert("");
			expressInbound.setInboundid(inbound.getId());
			expressInbound.setExpressid(entity.getId());
			expressInbound.setContractnumber(inbound.getRemark());
			expressInbound.setNumber(inbound.getDispatchnumber());
			sends += inbound.getDispatchnumber();
			ContractDetail detail = new ContractDetail();
			String[] trackcodes = inbound.getRemark().split(";");
			detail.setInboundid(inbound.getId());
			detail.setStatus("0");
			List<ContractDetail> details = contractDetailDao.findList(detail);
			for (int i = 0; i < inbound.getDispatchnumber(); i++) {
				detail = details.get(i);
				detail.setStatus("1");
				detail.setTracknumber(entity.getTracknumber());
				detail.setTrackcode(trackcodes[i]);
				ContractStatusHistroy histroy = buildStatusHistroy(detail);
				contractStatusHistroyDao.insert(histroy);
				contractDetailDao.update(detail);
			}
			int dispatchnumber = inbound.getDispatchnumber();
			expressInboundDao.insert(expressInbound);
			inbound = contractInboundDao.get(inbound);
			inbound.setDispatchnumber(
					dispatchnumber + (inbound.getDispatchnumber() == null ? 0 : inbound.getDispatchnumber()));
			contractInboundDao.update(inbound);
		}
		entity.setStatus("0");
		entity.setSends(sends);
		return expressInformationDao.insert(entity);
	}

	private ContractStatusHistroy buildStatusHistroy(ContractDetail detail) {
		ContractStatusHistroy histroy = new ContractStatusHistroy();
		histroy.setDetailid(detail.getId());
		Integer sequence = contractStatusHistroyDao.getMaxSequence(histroy);
		histroy.setSequence(sequence == null ? 1 : sequence + 1);
		histroy.preInsert("");
		histroy.setConfirtrackstatus(detail.getConfirtrackstatus() == null ? "0" : detail.getConfirtrackstatus());
		histroy.setRemark(detail.getRemarks());
		histroy.setStatus(detail.getStatus());
		histroy.setCreateBy(detail.getCreateBy());
		histroy.setUpdateDate(new Date());
		return histroy;
	}

	@Override
	public int update(ExpressInformation entity) {
		entity.preUpdate();
		return expressInformationDao.update(entity);
	}

	/**
	 * 撤销配送
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int delete(ExpressInformation entity) {
		ContractDetail detail = new ContractDetail();
		detail.setTracknumber(entity.getTracknumber());
		detail.setStatus("0");
		int i = contractDetailDao.update(detail);
		ExpressInbound expressInbound = new ExpressInbound();
		expressInbound.setExpressid(entity.getId());
		List<ExpressInbound> expressInbounds = expressInboundDao.findList(expressInbound);
		for (ExpressInbound express : expressInbounds) {
			ContractInbound contractInbound = new ContractInbound();
			contractInbound.setId(express.getInboundid());
			contractInbound = contractInboundDao.get(contractInbound);
			contractInbound.setDispatchnumber(contractInbound.getDispatchnumber() - express.getNumber());
			contractInboundDao.update(contractInbound);
		}
		entity.setStatus("3");
		return expressInformationDao.update(entity);
	}

	@Override
	public ExpressInformation get(ExpressInformation entity) {
		ExpressInformation ex = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setConfirtracknumber(ex.getTracknumber());
		List<ContractDetail> list = contractDetailDao.findList(detail);
		ex.setDetails(list);
		return ex;
	}

	/**
	 * 合同签收
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int trackSign(ExpressInformation entity) {
		entity = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setTracknumber(entity.getTracknumber());
		detail.setStatus("2");
		contractDetailDao.updateStatus(detail);
		entity.setSignBy(new EmpVO());
		entity.setSignDate(new Date());
		entity.setStatus("1");
		return expressInformationDao.update(entity);
	}

	/**
	 * 取消签收
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int trackSignCancel(ExpressInformation entity) {
		entity = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setTracknumber(entity.getTracknumber());
		detail.setStatus("1");
		contractDetailDao.updateStatus(detail);
		entity.setSignBy(new EmpVO());
		entity.setSignDate(new Date());
		entity.setStatus("0");
		return expressInformationDao.update(entity);
	}

	/**
	 * 合同回寄
	 * 
	 * @param information
	 * @return
	 */
	@Override
	public int saveReturn(ExpressInformation information) {
		// TODO Auto-generated method stub
		information.preInsert("");
		for (ContractDetail detail : information.getDetails()) {
			ContractDetail d = new ContractDetail();
			d.setId(detail.getId());
			d.setContractcode(detail.getContractcode());
			detail = contractDetailDao.get(d);
			detail.setReturntracknumber(information.getTracknumber());
			detail.setStatus("4");
			detail.setContractcode(d.getContractcode());
			ContractStatusHistroy histroy = buildStatusHistroy(detail);
			contractStatusHistroyDao.insert(histroy);
			contractDetailDao.update(detail);
		}
		information.setSends(information.getDetails().size());
		return expressInformationDao.insert(information);
	}

	/**
	 * 回寄签收
	 * 
	 * @param information
	 * @return
	 */
	@Override
	public int returnSign(ExpressInformation information) {
		// information.setStatus("1");
		int i = expressInformationDao.update(information);
		ContractDetail detail = new ContractDetail();
		detail.setReturntracknumber(information.getTracknumber());
		if (information.getStatus() != "1")
			return Integer.valueOf(information.getStatus());
		List<ContractDetail> list = contractDetailDao.findList(detail);
		for (ContractDetail dt : list) {
			dt.setStatus("5");
			contractDetailDao.updateStatus(dt);
			ContractStatusHistroy histroy = buildStatusHistroy(dt);
			contractStatusHistroyDao.insert(histroy);
		}
		return i;
	}

	@Override
	public int problemeContractReturn(ExpressInformation information) {
		information.preInsert("");
		List<ContractDetail> list = information.getDetails();
		ContractDetail detail = list.get(0);
		detail = contractDetailDao.get(detail);
		ExpressInformation ex = new ExpressInformation();
		ex.setTracknumber(detail.getReturntracknumber());
		ex = expressInformationDao.get(ex);
		ex.setStatus("4");
		ex.setReturnnumber(list.size());
		expressInformationDao.update(ex);
		for (ContractDetail de : list) {
			de = contractDetailDao.get(de);
			de.setProblemtracknumber(information.getTracknumber());
			de.setStatus("9");
			ContractStatusHistroy histroy = buildStatusHistroy(de);
			contractStatusHistroyDao.insert(histroy);
			contractDetailDao.updateStatus(de);
		}
		return expressInformationDao.insert(information);
	}

	@Override
	public int problemeSign(ExpressInformation information) {
		for (ContractDetail de : information.getDetails()) {
			de = contractDetailDao.get(de);
			de.setStatus("10");
			ContractStatusHistroy histroy = buildStatusHistroy(de);
			contractStatusHistroyDao.insert(histroy);
			contractDetailDao.updateStatus(de);
		}
		information.setStatus("1");
		return expressInformationDao.update(information);
	}

	/**
	 * 认购确认书签收
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int confirmationSign(ExpressInformation entity) {
		entity = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setConfirtracknumber(entity.getTracknumber());
		List<ContractDetail> list = contractDetailDao.findList(detail);
		for (ContractDetail contractDetail : list) {
			ContractDetail updDetail = new ContractDetail();
			updDetail.setId(updDetail.getId());
			updDetail.setConfirtrackstatus("7");
			contractDetailDao.updateAudit(contractDetail);
		}
		entity.setSignBy(new EmpVO());
		entity.setSignDate(new Date());
		entity.setStatus("1");
		return expressInformationDao.update(entity);
	}

	/**
	 * 取消认购确认书签收
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int confirmationSignCancel(ExpressInformation entity) {
		entity = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setConfirtracknumber(entity.getTracknumber());
		List<ContractDetail> list = contractDetailDao.findList(detail);
		for (ContractDetail contractDetail : list) {
			ContractDetail updDetail = new ContractDetail();
			updDetail.setId(updDetail.getId());
			updDetail.setConfirtrackstatus("5");
			contractDetailDao.updateAudit(contractDetail);
		}
		entity.setSignBy(new EmpVO());
		entity.setStatus("0");
		return expressInformationDao.update(entity);
	}

	@Override
	public int saveChangeEexpressInformation(ExpressInformation information) {
		// TODO Auto-generated method stub
		int number = 0;
		information.setType("4");// 调配
		information.setStatus("0");// 调配中
		information.setUpdateBy(information.getCreateBy());
		information.setUpdateDate(new Date());
		information.preInsert();
		expressInformationDao.insert(information);
		List<ContractInbound> inbounds = information.getInbounds();
		for (ContractInbound inboundsDetail : inbounds) {
			ContractInbound inbound = new ContractInbound();
			inbound.setProjectcode(inboundsDetail.getProjectcode());
			inbound.setDepartment(inboundsDetail.getDepartment());
			ContractInbound selInbound = contractInboundDao.get(inbound);
			ExpressInbound exInbound = new ExpressInbound();
			exInbound.preInsert();
			exInbound.setInboundid(selInbound.getId());
			exInbound.setExpressid(information.getId());
			exInbound.setNumber(inboundsDetail.getTotal());
			exInbound.setContractnumber(inboundsDetail.getContractpagecode());
			expressInboundDao.insert(exInbound);
			Appoint appoint = new Appoint();
			appoint.setStatus("5");
			appoint.setDepartmentcode(inboundsDetail.getDepartment());
			appoint.setProjectcode(inboundsDetail.getProjectcode());
			List<Appoint> selAppoint = appointDao.findList(appoint);
			String contractcodes = inboundsDetail.getContractpagecode();
			String[] contractcode = contractcodes.split(";");
			for (int i = 0; i < inboundsDetail.getTotal(); i++) {
				Appoint app = selAppoint.get(i);
				ContractDetail updContract = new ContractDetail();
				updContract.setReserveNo(app.getReserveNo());
				updContract.setTrackcode(contractcode[i]);
				updContract.setTracknumber(information.getTracknumber());
				updContract.setStatus("7");
				updContract.setProjectcode(inboundsDetail.getProjectcode());
				number += contractDetailDao.updateContractByReserve(updContract);
			}
		}
		return number;
	}

	@Override
	public Page<ExpressInformation> findListByType(Page<ExpressInformation> page, ExpressInformation entity) {
		// TODO Auto-generated method stub
		entity.setPage(page);
		page.setList(expressInformationDao.findListByType(entity));
		return page;
	}
	
	/**
	 * 调配撤销
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public int  returnExpressInformation(ExpressInformation entity) {
		entity = expressInformationDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setTracknumber(entity.getTracknumber());
		List<ContractDetail> list= contractDetailDao.findList(detail);
	    for (ContractDetail contractDetail : list) {
	    	contractDetail.setStatus("6");
	    	contractDetailDao.updateStatus(detail);
		}		
		entity.setUpdateBy(new EmpVO());
		entity.setUpdateDate(new Date());
		entity.setStatus("3");
		return expressInformationDao.update(entity);
	}

	@Override
	public Page<ExpressInformation> findDetails(Page<ExpressInformation> page,ExpressInformation information) {
		// TODO Auto-generated method stub
		information.setPage(page);
		page.setList(expressInformationDao.findDetails(information));
		return page;
	}

   
}
